package com.itheima.mp.query;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mp.domain.po.User;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@ApiModel(description = "分页查询参数")
public class PageQuery {
    @ApiModelProperty("当前页码")
    private Integer pageNo;
    @ApiModelProperty("每页条数")
    private Integer pageSize;
    @ApiModelProperty("排序字段")
    private String sortBy;
    @ApiModelProperty("是否升序")
    private Boolean isAsc;

    public  <T> Page<T> toMpPage(OrderItem ... orderItems){
        //1.  分页查询条件
        Page<T> page = Page.of(pageNo, pageSize);
        //2. 排序条件
        if(StrUtil.isNotBlank(sortBy)){
            page.addOrder(new OrderItem(sortBy,isAsc));
        }else{
            //默认排序
            page.addOrder(orderItems);
        }
        return page;
    }
    //OrderItem ... orderItems 对象不需要传 直接传里面的参数
    public  <T> Page<T> toMpPage(String defaultsortBy,boolean isAsc){
        return toMpPage(new OrderItem(defaultsortBy,isAsc));
     }
    public  <T> Page<T> toMpPageDefaultSortedByCreateTime(){
        return toMpPage(new OrderItem("create_time",false));
    }
    public  <T> Page<T> toMpPageDefaultSortedByUpdateTime(){
        return toMpPage(new OrderItem("update_time",false));
    }

}